package com.example.app_competition;


import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import android.content.SharedPreferences;

import java.util.HashMap; // 引入 HashMap 类

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "samples.flutter.dev/test";

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);

        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler(
                        (call, result) -> {

                            // This method is invoked on the main thread.
                            // TODO

                            //判断方法名是否支持
                            if(call.method.equals("saveSharedPreferences")) {
                                HashMap<String, String> Sites = call.arguments();

                                saveSharedPreferences(Sites);
                                result.success(0);
                                //String myString = "Hello from Android";
                                //result.success(myString);
                            }else if(call.method.equals("getSharedPreferences")) {
                                String key = call.arguments();

                                result.success(getSharedPreferencesKey(key));
                            }else if(call.method.equals("deleteSharedPreferences")) {
                                String key = call.arguments();
                                deleteSharedPreferencesKey(key);
                                result.success("删除");
                            } else {
                                result.notImplemented();
                            }
                        }
                );
    }


    public void saveSharedPreferences(HashMap<String, String> Sites){
        SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(Sites.get("key"), Sites.get("value"));
        editor.apply();
    }


    public String getSharedPreferencesKey(String key){
        SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }

    public void deleteSharedPreferencesKey(String key){
        SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();


        editor.remove(key);
        editor.apply();
    }


}